<template>
  <div id="QuestionsSubmitView">
    <a-form :model="searchParams" :layout="'inline'">
      <a-form-item field="title" label="标题" style="min-width: 200px">
        <a-input
          v-model="searchParams.title"
          placeholder="请输入题目标题"
          allow-clear
        />
      </a-form-item>
      <a-form-item field="language" label="语言">
        <a-select
          :style="{ width: '200px' }"
          placeholder="选择语言"
          v-model="searchParams.language"
          allow-clear
        >
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>go</a-option>
        </a-select>
      </a-form-item>
      <a-form-item
        field="userAccount"
        label="用户名称"
        style="min-width: 200px"
      >
        <a-input
          v-model="searchParams.userAccount"
          placeholder="请输入用户名称"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch">搜 索</a-button>
      </a-form-item>
    </a-form>
    <a-divider direction="horizontal" size="0px" />
    <a-table
      :columns="columns"
      :data="dataList"
      :stripe="true"
      :table-layout-fixed="true"
      :loading="isLoading"
      :pagination="{
        total: total,
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
      }"
      @page-change="onPageChange"
    >
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("yyyy-MM-DD HH:mm:ss") }}
      </template>
      <template #status="{ record }">
        <a-space wrap>
          <a-tag v-if="record.status === 0" color="grey">待判题</a-tag>
          <a-tag v-if="record.status === 1" color="blue">判题中</a-tag>
          <a-tag v-if="record.status === 2" color="green">已完成</a-tag>
          <a-tag v-if="record.status === 3" color="red">失败</a-tag>
        </a-space>
      </template>
      <template #judgeInfo="{ record }">
        <div>
          <a-tag color="#11264f">{{
            "结果: " + record.judgeInfo.message
          }}</a-tag>
          <a-tag color="#145b7d">{{
            "耗时: " + record.judgeInfo.time + " ms"
          }}</a-tag>
          <a-tag color="#009ad6">{{
            "内存: " + record.judgeInfo.memory + " kb"
          }}</a-tag>
        </div>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const dataList = ref([]);
const total = ref(0);
let isLoading = true;
const searchParams = ref<QuestionQueryRequest>({
  pageSize: 10,
  current: 1,
  title: "",
  userAccount: "",
  language: "",
});
const loadData = async () => {
  isLoading = true;
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    }
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
    isLoading = false;
  } else {
    message.error("查询失败," + res.message);
    isLoading = true;
  }
  isLoading = false;
};
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "标题",
    dataIndex: "questionVO.title",
  },
  {
    title: "语言",
    align: "center",
    dataIndex: "language",
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
  },
  {
    title: "提交用户",
    dataIndex: "userAccount",
  },
  {
    title: "判题状态",
    align: "center",
    slotName: "status",
  },
  {
    title: "提交时间",
    slotName: "createTime",
  },
];
const router = useRouter();
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};
watchEffect(() => {
  loadData();
});
const doSearch = () => {
  // 重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  loadData();
};
</script>

<style scoped>
#QuestionsSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
